package com.example.demo4.dao.impl;

import com.example.demo4.dao.StudentDao;
import com.example.demo4.entity.Student;
import com.example.demo4.util.JdbcUtil;

import java.sql.SQLException;
import java.util.List;
import java.util.Objects;

public class StudentDaolmpl implements StudentDao {

    @Override
    public boolean addStudent(Student student) {
        System.out.println(student);
        // id自动生成，state 状态 默认为1
        String sql = "INSERT INTO student(id,name,gender,birthday,clazz,state) VALUES(null,?,?,?,?,'1')";
        return JdbcUtil.updateTable(sql, student.getName(), student.getGender(), student.getBirthday(), student.getClazz());
    }

    @Override
    public Student getStudentById(int id) {
        String sql = "select * from student where id=?";
        return Objects.requireNonNull(JdbcUtil.queryTable(Student.class,sql,id).get(0));
    }

    @Override
    public List<Student> getAllStudent() {
        String sql = "select * from student";
        return JdbcUtil.queryTable(Student.class,sql);
    }
}
